import { Component, ElementRef, ViewChild } from '@angular/core';
import { NavController, IonicPage, Platform, NavParams } from 'ionic-angular';
import { localbase } from '../../models/localbase';

/**
 * Generated class for the QuestionCompletionPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */
declare let BMap;
declare let BMapLib;
@IonicPage()
@Component({
  selector: 'page-path-view',
  templateUrl: 'over-path.html',
})
export class OverPathPage {
  constructor(public navCtrl: NavController,
    private navparams: NavParams,
    public localbasefun: localbase
  ) {
  }
  points = [];
  StartDate = "";
  EndDate = "";
  _fqid;
  _fs;
  _ft;
  stime;
  dtime = "";
  stimes;
  dtimed = "";
  initdata: any;

  async ionViewWillEnter() {
    this._fqid = this.navparams.get("fqid");
    this._fs = this.navparams.get('fs');
    this._ft = this.navparams.get('ft');


    this.initdata =await this.cdata();
    // console.log(5555555555555)
    // console.log(this.initdata)

    // this.initdata=[
    //
    //     '116.432045,39.910683',
    //     '120.129721,30.314429',
    //     '121.491121,25.127053'
    // ]
    let map = new BMap.Map("container");

    map.enableContinuousZoom();//连续缩放效果，默认禁用
    map.enableScrollWheelZoom();//启动滚轮放大缩小，默认禁用
    if (this.initdata.length > 1) {
      let p = Math.ceil(this.initdata.length / 2);
      map.centerAndZoom(new BMap.Point(this.initdata[p].split(",")[0], this.initdata[p].split(",")[1]), 14);//设置中心和地图显示级别
      this.initdata.forEach((item,index)=>{
        let position = new BMap.Point(item.split(",")[0],item.split(",")[1]);
        this.points.push(position)
      })
    }
    if (this.initdata.length == 1) {
      map.centerAndZoom(new BMap.Point(this.initdata[0].split(",")[0],this.initdata[0].split(",")[1]), 14);
        let position = new BMap.Point(this.initdata[0].split(",")[0], this.initdata[0].split(",")[1]);
        this.points.push(position)
    }
    if(!this.initdata[0]){
      map.centerAndZoom(new BMap.Point(116.46,39.92), 7);
    }

    let curve = new BMapLib.CurveLine(this.points, { strokeColor: "blue", strokeWeight: 3, strokeOpacity: 0.5 }); //创建弧线对象
    map.addOverlay(curve); //添加到地图中
    curve.enableEditing(); //开启编辑功能
  }
  async cdata() {
    const getGpsarrdata = this.localbasefun;
    let gps = await getGpsarrdata.getGpsarr(this._fqid, this.dtime, this.dtimed);
    return gps
  }



  changeDate(e) {
    this.stime = e.replace('T', " ");
    this.dtime = this.stime.replace('Z', "");
    if (this.dtime) {
      this.cdata();
    }
  }
  changeDateend(e) {
    this.stimes = e.replace('T', " ");
    this.dtimed = this.stimes.replace('Z', "");
    if (this.dtimed) {
      this.cdata();
    }
  }
//清除tab
  ionViewDidEnter(){
    let elements = document.querySelectorAll(".tabbar");
    if (elements != null) {
      Object.keys(elements).map((key) => {
        elements[key].style.display = 'none';
      });
    }
  }


  ionViewWillLeave() {
    let elements = document.querySelectorAll(".tabbar");
    if (elements != null) {
      Object.keys(elements).map((key) => {
        elements[key].style.display = 'flex';
      });
    }
  }

}
